package engine.core
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.text.*;
	
	public class RxTextConsole
	{
		private var m_stage:Stage;
		private var m_visible:Boolean;
		private var m_rect:Rectangle;
		private var m_inputTextField:RxTextField;
		private var m_outputTextField:RxTextField;
		
		public function RxTextConsole(style:String, stage:Stage)
		{
			this.m_rect = new Rectangle();
			this.m_stage = stage;
			this.m_visible = false;
			this.m_inputTextField = new RxTextField(style, false, true);
			this.m_outputTextField = new RxTextField(style, true, false);
			SetColors(16777215, 0, 8421504, 0.8);
			var textFiled:TextField = this.m_inputTextField.GetTextField();
			textFiled.addEventListener(KeyboardEvent.KEY_DOWN, this.OnTextKeyDown)
		}
		
		public function SetColors(red:uint, gred:uint, blue:uint, alpha:Number) : void
		{
			m_inputTextField.SetColors(red, gred, blue, alpha);
			m_outputTextField.SetColors(red, gred, blue, alpha);
		}
		
		protected function OnTextKeyDown(e:KeyboardEvent) : void
		{
			var textField:TextField = m_inputTextField.GetTextField();
			
			switch(e.keyCode)
			{
				case RxKeyboard.KEY_ENTER:
					OnInput(m_inputTextField.GetText());
					break;
				case RxKeyboard.KEY_ESC:
					break;
				default:
					break;
			}
			
			e.stopImmediatePropagation();
		}
		
		protected function OnInput(str:String) : void
		{
			ClearInput();
		}
		
		public function ClearInput() : void
		{
			m_inputTextField.GetTextField().text = "";
		}
		
		public function SetRect(rect:Rectangle) : void
		{
			this.m_rect = rect;
			var m_rect:Rectangle = new Rectangle();
			var textHeight:* = this.m_inputTextField.GetTextField().textHeight;
			var offset:Number = 2;
			m_rect.x = rect.x;
			m_rect.y = rect.y + rect.height - 2 - textHeight - offset;
			m_rect.width = rect.width;
			m_rect.height = textHeight + 2 * offset;
			this.m_inputTextField.SetRect(m_rect);
			if (this.m_outputTextField != null)
			{
				m_rect.x = rect.x;
				m_rect.y = rect.y;
				m_rect.width = rect.width;
				m_rect.height = rect.height - textHeight - 2 * offset;
				this.m_outputTextField.SetRect(m_rect);
			}
		}
		
		public function GetRect() : Rectangle
		{
			return this.m_rect;
		}
		
		public function GetInput() : RxTextField
		{
			return this.m_inputTextField;
		}
		
		public function GetStage() : Stage
		{
			return this.m_stage;
		}
		
		public function ClearOutput() : void
		{
			this.m_outputTextField.GetTextField().text = "";
		}
		
		public function SetVisible(visible:Boolean) : void
		{
			if (visible != this.m_visible)
			{
				if (visible)
				{
					this.m_stage.addChild(this.m_inputTextField.GetTextField());
					this.m_stage.addChild(this.m_outputTextField.GetTextField());
					this.m_stage.focus = this.m_inputTextField.GetTextField();
				}
				else
				{
					this.m_stage.removeChild(this.m_inputTextField.GetTextField());
					this.m_stage.removeChild(this.m_outputTextField.GetTextField());
				}
				this.m_visible = visible;
			}
		}
		
		public function GetVisible() : Boolean
		{
			return m_visible;
		}
		
		public function Write(info:String) : void
		{
			this.m_outputTextField.AddText(info);
		}
		
		public function GetText() : String
		{
			return this.m_outputTextField.GetText();
		}
		
		public function GetInputText() : String
		{
			return this.m_inputTextField.GetText();
		}
		
		public function SetInputText(param1:String) : void
		{
			this.m_inputTextField.GetTextField().text = param1;
		}
		
		public function SetInputSelection(beginIndex:int, endIndex:int) : void
		{
			var textField:TextField = this.GetInput().GetTextField();
			
			if (beginIndex < 0) beginIndex = textField.length;
			
			if (endIndex < 0) endIndex = textField.length;
			
			textField.setSelection(beginIndex,endIndex);
		}
	}
}